package com.jsjvm.client.execution.objrepr;

import java.util.HashMap;

import com.jsjvm.client.classparser.JClass;
import com.jsjvm.client.classparser.JMember.JMethod;
import com.jsjvm.client.classparser.jconstants.JMemberConstant;
import com.jsjvm.client.execution.JClassLoader;
import com.jsjvm.client.execution.JClassLoader.JClassNotLoadedException;

/**
 * The way to access static members
 * 
 * TODO: lookup members 'per class' 
 * 
 * @author ctalau
 */
public class StaticMembers {
	private static HashMap<String, Object> global = new HashMap<String, Object>();
	private static HashMap<String, JMethod> funct = new HashMap<String, JMethod>();
	private static JClassLoader jcl = JClassLoader.getInstance();

	public static void putStaticField(JMemberConstant fld, Object value) throws JClassNotLoadedException {
		jcl.getClassByConstant(fld.getClassConstant());
		global.put(fld.getFullName(), value);
	}

	public static Object getStaticField(JMemberConstant fld) throws JClassNotLoadedException {
		jcl.getClassByConstant(fld.getClassConstant());
		return global.get(fld.getFullName());
	}

	public static JMethod dispatchMethod(JMemberConstant cm)
			throws JClassNotLoadedException {
		JMethod m = funct.get(cm.getFullName());

		if (m == null) {
			JClass jc = jcl.getClassByConstant(cm.getClassConstant());
			for (JMethod im : jc.getMethods()) {
				if (im.isStatic()) {
					funct.put(im.getFullName(), im);
				}
			}
			m = funct.get(cm.getFullName());
		}

		return m;
	}

}
